package com.tjsinfo.tjsoa.worklog.bo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.tjsinfo.tjsoa.common.DateTimeFromConver;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.TestRiQi;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.worklog.dao.IWorkLogMainDao;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogMain;
public class WorkLogMainBo  implements IWorkLogMainBo{
	final boolean  STARTFALSE=false;
	final boolean  STARTTRUE=true;
	private IWorkLogMainDao workLogMainDao;
	
	public IWorkLogMainDao getWorkLogMainDao() {
		return workLogMainDao;
	}

	public void setWorkLogMainDao(IWorkLogMainDao workLogMainDao) {
		this.workLogMainDao = workLogMainDao;
	}

	public void delete(String id) {
		// TODO Auto-generated method stub
		workLogMainDao.delete(selectByID(id));
	}

	public String save(Integer userid, Date datetime, String type,
			String content, String complete_countent, Integer year,
			Integer number, Integer start,Integer submittype) {
		// TODO Auto-generated method stub
		WorkLogMain workLogMain = new WorkLogMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workLogMain.setTjsUser(tjsUser);
		workLogMain.setCompleteContent(complete_countent);
		workLogMain.setContent(content);
		workLogMain.setTime(datetime);
		workLogMain.setType(type);
		workLogMain.setYear(year);
		workLogMain.setNumber(number);
		workLogMain.setStart(start);
		workLogMain.setSubmittype(submittype);
		String id = workLogMainDao.save(workLogMain);
		return id;
		
	}

	public WorkLogMain selectByID(String id) {
		// TODO Auto-generated method stub
		return workLogMainDao.selectOneById(id);
	}

	public WorkLogMain selectTypeAndUserAndDate(Integer userid, Date date,
			String type) {
		// TODO Auto-generated method stub
		
		return  workLogMainDao.selectDateTime(date,type, userid);
	}

	public WorkLogMain selectWorkLogMainAndExamine(String jihuaid) {
		// TODO Auto-generated method stub
		
		return workLogMainDao.selectWorkLog(jihuaid);
	}

	public List selectWorkLogTypeAndYear(String type, Integer year,
			Integer userid) {
		// TODO Auto-generated method stub
		
		try {
			WorkLogMain workLogMain = new WorkLogMain();
			workLogMain.setType(type);
			workLogMain.setYear(year);
			TSystemUser tjsUser = new TSystemUser();
			tjsUser.setUserId(userid);
			workLogMain.setTjsUser(tjsUser);
			
			return   workLogMainDao.selectWorkLogMainYearAndTypeAndUser(workLogMain);
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public Map<Date, WorkLogMain> selectWorkLogTypeAndYearMAP(String type,
			Integer year, Integer userid) {
		// TODO Auto-generated method stub
		
		Map<Date, WorkLogMain> map = new HashMap<Date, WorkLogMain>();
		List list =  selectWorkLogTypeAndYear(type, year,userid);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkLogMain workLogMain = (WorkLogMain) iterator.next();
			map.put(workLogMain.getTime(), workLogMain);
		}
		return map;
	}

	public Map<Integer, WorkLogMain> selectWorkLogTypeAndYearMAPNumber(
			String type, Integer year, Integer userid) {
		// TODO Auto-generated method stub
		Map<Integer, WorkLogMain> map = new HashMap<Integer, WorkLogMain>();
		List list =  selectWorkLogTypeAndYear(type, year,userid);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkLogMain workLogMain = (WorkLogMain) iterator.next();
			map.put(workLogMain.getNumber() , workLogMain);
		}
		return map;
	}

	public Map<Date, WorkLogMain> selectWorkLogTypeAndYearMAPTimeStart(
			String type, Integer year, Integer userid, Integer start) {
		// TODO Auto-generated method stub
		
		WorkLogMain workLogMain = new WorkLogMain();
		workLogMain.setType(type);
		workLogMain.setYear(year);
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workLogMain.setTjsUser(tjsUser);
		workLogMain.setStart(start);
		Map<Date, WorkLogMain> map = new HashMap<Date, WorkLogMain>();
		List list = workLogMainDao.selectWorkLogMainYearAndTypeAndUser(workLogMain);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkLogMain LogMain = (WorkLogMain) iterator.next();
			map.put(LogMain.getTime(), LogMain);
		}
		
		return map;
	}
	/**
	 * 检查默认工作日志日日志
	 */
	public Map<Date, WorkLogMain> selectDefantWorkLogDate(
			String type, Integer year, Integer userid, Integer start) {
		// TODO Auto-generated method stub
		
		WorkLogMain workLogMain = new WorkLogMain();
		workLogMain.setType(type);
		workLogMain.setYear(year);
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workLogMain.setTjsUser(tjsUser);
		workLogMain.setStart(start);
		Map<Date, WorkLogMain> map = new HashMap<Date, WorkLogMain>();
		workLogMain.setSubmittype(0);
		//List list = selectWorkLogTypeAndYearListNumberStart(workLogMain);
		List list = workLogMainDao.selectDefanultDateWorkLog(workLogMain);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkLogMain LogMain = (WorkLogMain) iterator.next();
			map.put(LogMain.getTime(), LogMain);
		}
		
		return map;
	}

	public Map<Integer, WorkLogMain> selectWorkLogTypeAndYearMAPNumberStart(
			String type, Integer year, Integer userid, Integer start) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		WorkLogMain workLogMain = new WorkLogMain();
		workLogMain.setType(type);
		workLogMain.setYear(year);
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workLogMain.setTjsUser(tjsUser);
		workLogMain.setStart(start);
		workLogMain.setSubmittype(0);
		Map<Integer, WorkLogMain> map = new HashMap<Integer, WorkLogMain>();
		List list = workLogMainDao.selectDefanultDateWorkLog(workLogMain);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkLogMain LogMain = (WorkLogMain) iterator.next();
			map.put(LogMain.getNumber(), LogMain);
		}
		
		return map;
	}

	public void update(String id, Integer userid, Date datetime, String type,
			String content, String complete_countent, Integer year,
			Integer number, Integer start) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		WorkLogMain workLogMain = new WorkLogMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workLogMain =  workLogMainDao.selectOneById(id);
		workLogMain.setTjsUser(tjsUser);
		workLogMain.setCompleteContent(complete_countent);
		workLogMain.setContent(content);
		workLogMain.setTime(datetime);
		workLogMain.setType(type);
		
		workLogMain.setYear(year);
		workLogMain.setStart(start);
		workLogMain.setNumber(number);
		workLogMainDao.update(workLogMain);
		
	}
	
	private  List selectWorkLogTypeAndYearListNumberStart(WorkLogMain workLogMain)
	{
		List list =  workLogMainDao.selectWorkLogMainYearAndTypeAndStart(workLogMain);
		return list;
	}

	public List selectTypeAndUser(String type, Integer id) {
		// TODO Auto-generated method stub
		WorkLogMain logMain = new WorkLogMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(id);
		logMain.setType(type);
		logMain.setTjsUser(tjsUser);
		return workLogMainDao.selectWorkLogMainUserAndType(logMain);
		
	}
	/**
	 * 模糊查询
	 * @param userid
	 * @param type
	 * @param content
	 * @return
	 */
	public List selectLike(Integer userid,String content) {
		// TODO Auto-generated method stub
		WorkLogMain workLogMain = new WorkLogMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workLogMain.setTjsUser(tjsUser);
		workLogMain.setContent(content);
		return workLogMainDao.selectLikeWorkLog(workLogMain);
		 
	}

	/**
	 * 根据类型模糊查询
	 * @param userid
	 * @param type
	 * @param content
	 * @return
	 */
	public List selectLikeType(Integer userid, String type,String content) {
		// TODO Auto-generated method stub
		WorkLogMain workLogMain = new WorkLogMain();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		workLogMain.setTjsUser(tjsUser);
		workLogMain.setContent(content);
		workLogMain.setType(type);
		return workLogMainDao.selectLikeWorkLogType(workLogMain);
	}
}
